package com.zifan.yygh.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zifan.common.result.Result;
import com.zifan.yygh.exception.YyghException;
import com.zifan.yygh.model.hosp.HospitalSet;
import com.zifan.yygh.service.HospitalSetService;
import com.zifan.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author 子凡
 * @since 2021-09-23
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置表所有信息
     * @return 医院信息
     */
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        //模拟异常
        try {
            int a = 1/0;
        }catch (Exception e){
            throw new YyghException("出错了", 201);
        }

        return Result.ok(hospitalSetService.list());
    }

    /**
     * 逻辑删除医院设置
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据条件，分页获取数据
     */
    @ApiOperation(value = "根据条件，分页获取数据")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(
            @PathVariable("current") long current,
            @PathVariable("limit") long limit,
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo
    ) {
        /**
         * 这里我觉得有必要再说一下我对controller和service层的理解
         * 看见弹幕有好多再说业务代码到底要不要写在controller里面
         * 我个人认为，controller做的是：处理接收数据，处理返回数据
         * 而service只做业务相关的事情，我的代码风格是这样的
         */

        return Result.ok(hospitalSetService.findPageHospSet(current, limit, hospitalSetQueryVo));

    }

    /**
     * 添加医院设置
     * 其实代码都好实现，不过就是要遵守他的命名。。
     * 测试数据
     * {
     *   "apiUrl": "localhost:9999",
     *   "contactsName": "李四",
     *   "contactsPhone": "95003",
     *   "hoscode": "1000_6",
     *   "hosname": "测试数据"
     * }
     */
    @PostMapping("saveHospitalSet")
    @ApiOperation(value = "添加医院设置")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        return hospitalSetService.saveHospitalSet(hospitalSet) ? Result.ok() : Result.fail();
    }


    /**
     * 根据id获取医院设置
     */
    @GetMapping("getHospSet/{id}")
    @ApiOperation(value = "根据id获取医院设置")
    public Result getHospSet(@PathVariable Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    /**
     * 修改医院设置
     */
    @PostMapping("updateHospitalSet")
    @ApiOperation(value = "修改医院设置")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除医院设置
     */
    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删除医院设置")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        return hospitalSetService.removeByIds(idList) ? Result.ok() : Result.fail();
    }

    /**
     * 医院设置锁定和解锁
     */
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        UpdateWrapper<HospitalSet> updateWrapper = new UpdateWrapper<>();

        //构造修改条件
        updateWrapper.lambda().set(HospitalSet::getStatus, status)
                .eq(HospitalSet::getId, id);

        return hospitalSetService.update(updateWrapper) ? Result.ok() : Result.fail();
    }

    /**
     * 发送签名秘钥
     */
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //todo 发送短信
        return Result.ok();
    }



}

